//
// Created by wangjunjun on 2024/2/19.
//

#include <mutex>
#include <iostream>
#include <string>
#include "log.h"
#include <mutex>
#include <iostream>
std::shared_ptr<CLog> CLog::m_pInstance = nullptr;
std::once_flag CLog::mflag;

std::shared_ptr<CLog> CLog::GetInstance() {
    std::call_once(mflag, []() {
        m_pInstance = std::shared_ptr<CLog>(new CLog(),
                                            [](CLog* ptr){delete ptr;});
    });

    return m_pInstance;
}
CLog:: CLog()
{
    // 加载配置文件
    // 需要能够动态调整打印等级
    try
    {
        log4cpp::PropertyConfigurator::configure("./CFG/log4cfg");
    }
    catch (log4cpp::ConfigureFailure & f)
    {
        std::cerr << "configure problem " << f.what() << std::endl;
    }
//    if (log4cpp::Category::exists("log"))
//    {
//        std::cout << "Category::exists " << "log" << std::endl;
//    }
//    else
//    {
//        std::cerr << "Category::exists error " << "log" << std::endl;
//    }
//    if (log4cpp::Category::exists("dataCapture1"))
//    {
//        std::cout << "Category::exists " << "dataCapture1" << std::endl;
//    }
//    else
//    {
//        std::cerr << "Category::exists error " << "dataCapture1" << std::endl;
//    }
}

CLog::~CLog()
{
    log4cpp::Category& log = log4cpp::Category::getRoot();
    log.shutdown();
}

void  CLog::logDebug(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("log");
    log.debug(strMsg);
}

void  CLog::logError(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("log");
    log.error(strMsg);
}

void  CLog::logInfo(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("log");
    log.info(strMsg);
}

void  CLog::logWarn(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("log");
    log.warn(strMsg);
}

void  CLog::logFatal(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("log");
    log.fatal(strMsg);
}

void  CLog::dataCapture1(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("dataCapture1");
    log.info(strMsg);
}
